/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package button_tools;

import java.awt.Color;
import java.awt.geom.Point2D;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
/**
 *
 * @author tnull
 */

// FILE HANDLER
// takes care of saving and loading paint projects
public class TFileHandler {

    // copy of the settings so we can access its data
    public static TPaintSettings m_Settings;

    // constructor - takes a TPaintSettings object to copy
    public TFileHandler(TPaintSettings settings) {
        m_Settings = settings;
        
    }

    // open the file, read it, and create the objects
    // filename or extension is not important
    public static void open(String fileName) {

        // init everything
        File theFile = new File(fileName);
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        DataInputStream dis = null;
        // ensure the current buffer is clear.
        m_Settings.m_DrawBuffer.clear();
        
        // try opening the various input streams
        try {
            fis = new FileInputStream(theFile);
            bis = new BufferedInputStream(fis);
            dis = new DataInputStream(bis);
        
        } catch (FileNotFoundException fnfe) {
            System.out.println(fnfe.getMessage());

        }
        
        try {
            // while data is available
            while (dis.available() != 0) {
                // read a line
                String aLine = dis.readLine();
                //split string based on the equals, first part will be the
                // shape type, second is the values.
                String[] splitString = aLine.split("=");
                String name = splitString[0];
                // split all of the values based on the comma
                String[] values = splitString[1].split(",");

                
 // PARSE THROUGH THE NAME, CREATE THE SHAPE BASED ON ITS VALUES
 /* and add it to the buffer
  * First value should be the color in rgb format
  * Second value is the x coordinate of the first point
  * Thrid value is the y coordinate of the first point
  * fourth value is the x coordinate of the second point
  * fifth value is the y coordinate of the second point
  * sixth value is the boolean value for filled
  */
                if (name.equals("RECTANGLE")) {
                    String color = values[0];
                    int shapeX = Integer.parseInt(values[1]);
                    int shapeY = Integer.parseInt(values[2]);
                    int shapeW = Integer.parseInt(values[3]);
                    int shapeH = Integer.parseInt(values[4]);
                    boolean filled = Boolean.parseBoolean(values[5]);

                    TRectangle rec = new TRectangle(Color.decode(color), shapeX, shapeY, shapeW, shapeH);
                    rec.setFilled(filled);
                    m_Settings.m_DrawBuffer.add(rec);

                } else if (name.equals("CIRCLE")) {
                    String color = values[0];
                    int shapeX = Integer.parseInt(values[1]);
                    int shapeY = Integer.parseInt(values[2]);
                    int shapeW = Integer.parseInt(values[3]);
                    int shapeH = Integer.parseInt(values[4]);
                    boolean filled = Boolean.parseBoolean(values[5]);

                    TRectangle rec = new TRectangle(Color.decode(color), shapeX, shapeY, shapeW, shapeH);
                    rec.setFilled(filled);
                    m_Settings.m_DrawBuffer.add(rec);

                } else if (name.equals("LINE")) {
                    String color = values[0];
                    int shapeX = Integer.parseInt(values[1]);
                    int shapeY = Integer.parseInt(values[2]);
                    int shapeW = Integer.parseInt(values[3]);
                    int shapeH = Integer.parseInt(values[4]);
                    boolean filled = Boolean.parseBoolean(values[5]);

                    TRectangle rec = new TRectangle(Color.decode(color), shapeX, shapeY, shapeW, shapeH);
                    rec.setFilled(filled);
                    m_Settings.m_DrawBuffer.add(rec);
                
                } else if (name.equals("BRUSH")) {
                    String color = values[0];
                    int shapeX = Integer.parseInt(values[1]);
                    int shapeY = Integer.parseInt(values[2]);
                    int shapeW = Integer.parseInt(values[3]);
                    int shapeH = Integer.parseInt(values[4]);
                    boolean filled = Boolean.parseBoolean(values[5]);
                    
                    TBrush brush = new TBrush(Color.decode(color), shapeX, shapeY, shapeW, shapeH);
                    brush.setFilled(filled);
                    m_Settings.m_DrawBuffer.add(brush);
                  
                    // the triangle works the same as the rest
                    // but requires 3 points instead of two
                } else if (name.equals("TRIANGLE")) {
                    String color = values[0];
                    double shapeX1 = Double.parseDouble(values[1]);
                    double shapeY1 = Double.parseDouble(values[2]);
                    double shapeX2 = Double.parseDouble(values[3]);
                    double shapeY2 = Double.parseDouble(values[4]);
                    double shapeX3 = Double.parseDouble(values[5]);
                    double shapeY3 = Double.parseDouble(values[6]);
                    boolean filled = Boolean.parseBoolean(values[7]);

                    Point2D p1 = new Point2D.Double(shapeX1, shapeY1);
                    Point2D p2 = new Point2D.Double(shapeX2, shapeY2);
                    Point2D p3 = new Point2D.Double(shapeX3, shapeY3);

                    TTriangle tri = new TTriangle(Color.decode(color), p1, p2, p3);
                    tri.setFilled(filled);
                    m_Settings.m_DrawBuffer.add(tri);
            
                }
                m_Settings.m_fileloaded = true;
   
            }
            // catch crappy errors
        } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
        } catch (NullPointerException npe) {
            System.out.println(npe.getMessage());
        }

        // shut 'er down
        try {
            dis.close();
            bis.close();
            fis.close();

        } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
        }
    }

    // SAVE TO A specified filename
    // filename or extension is not important
    public static void save(String fileName) {

        File theFile = new File(fileName);
        FileOutputStream fos = null;
        PrintStream ps = null;

        // try to open a printstream and output stream
        try {
            fos = new FileOutputStream(theFile);
            ps = new PrintStream(fos);
            

        } catch (FileNotFoundException fnfe) {
            System.out.println(fnfe.getMessage());

        }
        // Loop through each instace of shape in the drawing buffer
            for (TShape aShape : m_Settings.m_DrawBuffer) {
                // our writable
                String entry;
                // TRIANGLES ARE SPECIAL...retarded
                // pull the value of the points, color, shape type, and fill
                if (aShape.getType() == TShape.SHAPE_TYPE.TRIANGLE) {
                    String type = aShape.getType().toString();
                    Point2D[] parray = ((TTriangle)aShape).getPoints();
                    int shapeColor = aShape.getColor();
                    double shapeX1 = parray[0].getX();
                    double shapeY1 = parray[0].getY();
                    double shapeX2 = parray[1].getX();
                    double shapeY2 = parray[1].getY();
                    double shapeX3 = parray[2].getX();
                    double shapeY3 = parray[2].getY();
                    //nt shapeW = aShape.getM_width();
                    //int shapeH = aShape.getM_height();
                    boolean filled = aShape.getFilled();
                    
                    // prepare the entry to be written, comma separated values
                    entry = type + "=" + shapeColor + "," + shapeX1 + "," + shapeY1 + "," + shapeX2 + "," + shapeY2
                            + "," + shapeX3 + "," + shapeY3 + "," + filled;
                            
                    // ALL OTHER SHAPES GET THIS ONE
                    // ONLY RETARDED TRIANGLES GET THE ABOVE
                    // same concept, pull the two point values (x,y), color, and filled
                } else {
                    String type = aShape.getType().toString();
                    int shapeColor = aShape.getColor();
                    int shapeX = aShape.getM_x();
                    int shapeY = aShape.getM_y();
                    int shapeW = aShape.getM_width();
                    int shapeH = aShape.getM_height();
                    boolean filled = aShape.getFilled();
                    
                    // prepare entry to be written
                    entry = type + "=" + shapeColor + "," + shapeX + "," + shapeY + "," + shapeW + "," + shapeH + "," + filled;
               }
                // write the entry to the file.                                       
                ps.println(entry);                                             
           }
           
        // shut 'er down
        try {
           ps.close();
           fos.close();

        } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
        }
    }

    // why is this here?
    private class TParser {
        
        
    }
}
